package com.taomus.browser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.wb.swt.SWTResourceManager;

public class BrowserShell extends Shell {
	private Text location;
	private Map<String, Object> objMap = new HashMap<String, Object>();

	/**
	 * Create the shell.
	 * 
	 * @param display
	 */
	public BrowserShell(Display display) {
		super(display, SWT.SHELL_TRIM | SWT.BORDER);
		setLayout(new FormLayout());
		GeckoBrowser browser = new GeckoBrowser(this);
		FormData fd_browser = new FormData();
		fd_browser.bottom = new FormAttachment(100, -23);
		fd_browser.top = new FormAttachment(0, 33);
		fd_browser.right = new FormAttachment(100);
		fd_browser.left = new FormAttachment(0);
		browser.setLayoutData(fd_browser);

		Button backBtn = new Button(this, SWT.NONE);
		FormData fdBackBtn = new FormData();
		fdBackBtn.top = new FormAttachment(0);
		fdBackBtn.left = new FormAttachment(0);
		backBtn.setLayoutData(fdBackBtn);
		backBtn.setText("<");
		browser.back(backBtn);

		Button forwardBtn = new Button(this, SWT.NONE);
		FormData fdForwordBtn = new FormData();
		fdForwordBtn.top = new FormAttachment(backBtn, 0, SWT.TOP);
		fdForwordBtn.left = new FormAttachment(backBtn, 6);
		forwardBtn.setLayoutData(fdForwordBtn);
		forwardBtn.setText(">");
		browser.forward(forwardBtn);

		Button btnRefresh = new Button(this, SWT.NONE);
		FormData fdBtnRefresh = new FormData();
		fdBtnRefresh.left = new FormAttachment(backBtn, 33);
		fdBtnRefresh.top = new FormAttachment(backBtn, 0, SWT.TOP);
		btnRefresh.setLayoutData(fdBtnRefresh);
		browser.refresh(btnRefresh);

		location = new Text(this, SWT.BORDER);
		FormData fdLocation = new FormData();
		fdLocation.right = new FormAttachment(100, -50);
		fdLocation.top = new FormAttachment(0, 4);
		fdLocation.left = new FormAttachment(0, 157);
		location.setLayoutData(fdLocation);
		location.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
				if (e.keyCode == SWT.CR) {
					browser.setUrl(location.getText());
				}
			}
		});

		Button btnGo = new Button(this, SWT.NONE);
		FormData fdBtnGo = new FormData();
		fdBtnGo.left = new FormAttachment(100, -45);
		fdBtnGo.top = new FormAttachment(0, 2);
		fdBtnGo.right = new FormAttachment(100, -10);
		btnGo.setLayoutData(fdBtnGo);
		btnGo.setText("go");
		browser.go(btnGo, location);

		Button btnStop = new Button(this, SWT.NONE);
		FormData fdBtnStop = new FormData();
		fdBtnStop.top = new FormAttachment(backBtn, 0, SWT.TOP);
		fdBtnStop.left = new FormAttachment(btnRefresh, 6);
		btnStop.setLayoutData(fdBtnStop);
		btnStop.setText("✘");
		browser.stop(btnStop);

		Label lblloc = new Label(this, SWT.NONE);
		FormData fd_lblNewLabel = new FormData();
		fd_lblNewLabel.bottom = new FormAttachment(100);
		fd_lblNewLabel.right = new FormAttachment(100, -120);
		fd_lblNewLabel.left = new FormAttachment(0);
		lblloc.setLayoutData(fd_lblNewLabel);
		lblloc.setText("info");
	    browser.setLeftInfo(lblloc);

		Label lblhr = new Label(this, SWT.SEPARATOR | SWT.HORIZONTAL);
		FormData fd_label_2 = new FormData();
		fd_label_2.left = new FormAttachment(0);
		fd_label_2.right = new FormAttachment(100);
		fd_label_2.bottom = new FormAttachment(100, -17);
		lblhr.setLayoutData(fd_label_2);
		this.thislistener(this);
		createContents();
	}

	private void thislistener(Shell shell) {
		this.addShellListener(new ShellAdapter() {
			@Override
			public void shellClosed(ShellEvent e) {
				MessageBox mb = new MessageBox(shell,SWT.ICON_QUESTION | SWT.OK| SWT.CANCEL);
				mb.setText("提示");
				mb.setMessage("确定要关闭吗?");
				int rc = mb.open();
				if (e.doit == (rc == SWT.OK)) {
					System.exit(0);
				} else if (e.doit == (rc == SWT.CANCEL)) {
					Display display = shell.getDisplay();
					shell.open();
					shell.layout();
					while (!shell.isDisposed()) {
						if (!display.readAndDispatch()) {
							display.sleep();
						}
					}
					shell.close();
				}
			}
		});
	}

	/**
	 * Create contents of the shell.
	 */
	protected void createContents() {
		setText("MyBrowser");
		setSize(800, 600);

	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
